package com.ucenter.client.bizbase.sys.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.monitor.api.ApiInfo;
import com.monitor.api.ApiParam;
import com.monitor.api.ApiRes;
import com.ms.client.bizbase.sys.pojo.SysDict;
import com.ms.client.bizbase.sys.service.SysDictService;
import com.ms.msclient.MsClientResponse;
import com.system.comm.model.Page;
import com.system.comm.utils.FrameJsonUtil;
import com.system.handle.model.ResponseCode;

/**
 * 字典的操作
 * @author yuejing
 * @date 2016-05-22 11:17:54
 * @version V1.0.0
 */
@RestController
@RequestMapping(value = "/sysDict")
public class SysDictController {

	private static final Logger LOGGER = LoggerFactory.getLogger(SysDictController.class);
	@Autowired
	private SysDictService sysDictService;

	/**
	 * 获取对象
	 * @param params
	 * @return
	 */
	@RequestMapping(name = "字典-字典获取对象", value = "/get/{typeCode}/{dictId}")
	@ApiInfo(params = {
			@ApiParam(name="类型编码", code="typeCode", clazz=String.class, value="GENDER"),
			@ApiParam(name="字典值编码", code="dictId", clazz=String.class, value="WOMAN")
	},
	response = {
			@ApiRes(name="响应码", code="code", clazz=String.class, value="0"),
			@ApiRes(name="响应消息", code="message", clazz=String.class, value="success"),
			@ApiRes(name="主体内容", code="body", clazz=Object.class, value=""),
			@ApiRes(name="字典编号", code="dictId", pCode="body", clazz=String.class, value="roleIdClientManager"),
			@ApiRes(name="分类编号", code="typeCode", pCode="body", clazz=String.class, value="ROLE_INFO")
	})
	public MsClientResponse<SysDict> get(@PathVariable String typeCode,
			@PathVariable String dictId) {
		try {
			MsClientResponse<SysDict> frame = sysDictService.get(typeCode, dictId);
			SysDict dict = frame.getBody();
			LOGGER.info("dict: {}", FrameJsonUtil.toString(dict));
			return frame;
		} catch (Exception e) {
			LOGGER.error("处理业务异常: " + e.getMessage(), e);
			return new MsClientResponse<>(ResponseCode.SERVER_ERROR);
		}
	}

	@RequestMapping(name = "字典-保存或修改", value = "/saveOrUpdate")
	@ApiInfo(params = {
			@ApiParam(name="类型编码", code="typeCode", clazz=String.class, value="GENDER"),
			@ApiParam(name="字典值编码", code="dictId", clazz=String.class, value="WOMAN"),
			@ApiParam(name="名称", code="name", clazz=String.class, value="女"),
			@ApiParam(name="值", code="value", clazz=String.class, value="0"),
			@ApiParam(name="备注", code="remark", clazz=String.class, value="备注"),
			@ApiParam(name="当前登录人ID", code="currentUserId", clazz=String.class, value="1"),
	},
	response = {
			@ApiRes(name="响应码", code="code", clazz=String.class, value="0"),
			@ApiRes(name="响应消息", code="message", clazz=String.class, value="success"),
			@ApiRes(name="主体内容", code="body", clazz=Object.class, value=""),
	})
	public MsClientResponse<SysDict> saveOrUpdate(@RequestBody SysDict sysDict) {
		try {
			MsClientResponse<SysDict> frame = sysDictService.saveOrUpdate(sysDict);
			return frame;
		} catch (Exception e) {
			LOGGER.error("处理业务异常: " + e.getMessage(), e);
			return new MsClientResponse<>(ResponseCode.SERVER_ERROR);
		}
	}

	@RequestMapping(name = "字典-查询列表", value = "/findByTypeCode")
	@ApiInfo(params = {
			@ApiParam(name="类型编码", code="typeCode", clazz=String.class, value="GENDER"),
	},
	response = {
			@ApiRes(name="响应码", code="code", clazz=String.class, value="0"),
			@ApiRes(name="响应消息", code="message", clazz=String.class, value="success"),
			@ApiRes(name="主体内容", code="body", clazz=Object.class, value=""),
			@ApiRes(name="编号", code="dictId", pCode="body", value=""),
			@ApiRes(name="类型编码", code="typeCode", pCode="body", value=""),
			@ApiRes(name="名称", code="name", pCode="body", value=""),
			@ApiRes(name="值", code="value", pCode="body", value=""),
			@ApiRes(name="备注", code="remark", pCode="body", value=""),
			@ApiRes(name="添加人", code="addUserId", pCode="body", value=""),
			@ApiRes(name="添加时间", code="createTime", pCode="body", value=""),
	})
	public MsClientResponse<SysDict> findByTypeCode(String typeCode) {
		try {
			MsClientResponse<SysDict> frame = sysDictService.findByTypeCode(typeCode);
			List<SysDict> data = frame.getBody();
			LOGGER.info("SysDicts: {}", FrameJsonUtil.toString(data));
			return frame;
		} catch (Exception e) {
			LOGGER.error("处理业务异常: " + e.getMessage(), e);
			return new MsClientResponse<>(ResponseCode.SERVER_ERROR);
		}
	}
	
	@RequestMapping(name = "字典-分页查询列表", value = "/pageQuery")
	@ApiInfo(params = {@ApiParam(name="页面", code="page", value="1"),
			@ApiParam(name="每页大小", code="size", value="10"),
			@ApiParam(name="排序[{\"property\": \"createTime\", \"type\":\"desc\", \"order\":1}]", code="orderby", value="", required=false),
			@ApiParam(name="类型编码", code="typeCode", clazz=String.class, value="GENDER"),
	},
	response = {
			@ApiRes(name="响应码", code="code", clazz=String.class, value="0"),
			@ApiRes(name="响应消息", code="message", clazz=String.class, value="success"),
			@ApiRes(name="主体内容", code="body", clazz=Object.class, value=""),
			@ApiRes(name="当前页码", code="page", pCode="body", clazz=Integer.class, value="1"),
			@ApiRes(name="每页大小", code="size", pCode="body", clazz=Integer.class, value="10"),
			@ApiRes(name="总页数", code="totalPage", pCode="body", clazz=Integer.class, value="5"),
			@ApiRes(name="总记录数", code="total", pCode="body", clazz=Integer.class, value="36"),
			@ApiRes(name="数据集合", code="rows", pCode="body", clazz=List.class, value=""),
			@ApiRes(name="编号", code="dictId", pCode="rows", value=""),
			@ApiRes(name="类型编码", code="typeCode", pCode="rows", value=""),
			@ApiRes(name="名称", code="name", pCode="rows", value=""),
			@ApiRes(name="值", code="value", pCode="rows", value=""),
			@ApiRes(name="备注", code="remark", pCode="rows", value=""),
			@ApiRes(name="添加人", code="addUserId", pCode="rows", value=""),
			@ApiRes(name="添加时间", code="createTime", pCode="rows", value=""),
	})
	public MsClientResponse<Page<SysDict>> pageQuery(SysDict sysDict, String orderby) {
		try {
			/*if(FrameStringUtil.isNotEmpty(orderby)) {
				List<Orderby> orderbys = FrameJsonUtil.toList(orderby, Orderby.class);
				sysDict.setOrderbys(orderbys);
			}*/
			MsClientResponse<Page<SysDict>> frame = sysDictService.pageQuery(sysDict, orderby);
			return frame;
		} catch (Exception e) {
			LOGGER.error("处理业务异常: " + e.getMessage(), e);
			return new MsClientResponse<>(ResponseCode.SERVER_ERROR);
		}
	}
	
	/*

	@RequestMapping(value = "/sysDict/delete")
	public ResponseFrame delete(String dictId) {
		try {
			ResponseFrame frame = sysDictService.delete(dictId);
			return frame;
		} catch (Exception e) {
			LOGGER.error("处理业务异常: " + e.getMessage(), e);
			return new ResponseFrame(ResponseCode.SERVER_ERROR);
		}
	}
*/
}